package com.offcn.sellergood.controller;

import com.offcn.entity.PageResult;
import com.offcn.entity.Result;
import com.offcn.entity.StatusCode;
import com.offcn.sellergood.pojo.Brand;
import com.offcn.sellergood.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @GetMapping
    public Result<List<Brand>> fingAll(){
        List<Brand> list = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功了",list);
    }
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable Long id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK,"通过id查询成功了",brand);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK,"增加成功了",null);
    }

    //gai
    @PutMapping("/{id}")
    public Result update(@PathVariable Long id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.updateBrand(brand);
        return new Result(true,StatusCode.OK,"修改成功",null);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){

        brandService.delete(id);
        return new Result(true,StatusCode.OK,"终于删除成功了",null);
    }
///////////////////////////////////////////////////
    @PostMapping("/search")
    public Result<List<Brand>> search(@RequestBody(required = false) Brand brand){
        List<Brand> list = brandService.findList(brand);
        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功",list);
    }

   /* @GetMapping("/search/{pageNo}/{pageSize}")
    public Result<PageResult<Brand>>findByPage(@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        PageResult<Brand> page = brandService.findPage(pageNo, pageSize);
        return new Result<PageResult<Brand>>(true,StatusCode.OK,"查询成功",page);
    }*/

    //分页、条件加查询
    @GetMapping("/search/{pageNo}/{pageSize}")
    public Result<PageResult<Brand>>findByPage(@RequestBody(required = false) Brand brand ,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
        PageResult<Brand> page = brandService.findPage(brand,pageNo, pageSize);
        return new Result<PageResult<Brand>>(true,StatusCode.OK,"查询成功",page);
    }

    @GetMapping("/selectOptions")
    public List<Map>selectOptions(){
        return brandService.selectOptions();
    }
}
